<?php
include_once $_SERVER['DOCUMENT_ROOT']."/math-videos/configure.inc.php";
if(isset($_REQUEST['id'] ) && $_REQUEST['id'] !='' && is_numeric($_REQUEST['id'])  && strtoupper($_REQUEST['del']) =='Y' )
{
  $id = $_REQUEST['id'];
  if($_SESSION['admin'])
	{
		//get image informatjion
		
		$sqltext="select title, 
											abstract, 
											to_char(asset.lecture_date,'mm/dd/yyyy') as lecture_date,  
											duration, 
											asset_type, 
											abstract_url, 
											remote_url, 
											location, 
											event_title, 
											to_char(asset.created_datetime,'mm/dd/yyyy') as created_datetime, 
											created_by,
											to_char(asset.last_modified_datetime,'mm/dd/yyyy') as last_modified_datetime,
											 last_modified_by,
											rating, 
											views,
											thumbnail,
											thumbnail_type,
											assetSHA1,
											timecodeSHA1,
											institute_id
											from asset WHERE id=?";
		$params = array();
		
		array_push($params,sql_escape($id));
	//	array_push($params,sql_escape($user_id));
		$result = db_select_query($conn,$sqltext,$params);
		while($row = db_fetch_object($result))
		{
			foreach ($row as $key => $value) 
			{
				$value = trim($value);
				$$key = $value;
			}
		}		
		if($thumbnail_type !='' && $thumbnail!='')//has thumbail
		{
		 $image_name =$id."_thumb.".$thumbnail_type;
		 $thumbnail_image_file = $_SERVER['DOCUMENT_ROOT']."/math-videos/images/".$institute_id."/".$id."/".$image_name;	
		 if(file_exists($thumbnail_image_file))
		 {
			  unlink($thumbnail_image_file);
		 }
		}
		
		$sqltext="delete 
		from asset where id=?";
		$params = array();	
		array_push($params,sql_escape($id));
		db_change_query($conn,$sqltext,$params);
	
		$sqltext="delete 
		from presenter where asset_id=?";
		$params = array();
		array_push($params,sql_escape($id));
		db_change_query($conn,$sqltext,$params);
		$sqltext="delete 
		from keyword where asset_id=?";
		$params = array();
		array_push($params,sql_escape($id));
		db_change_query($conn,$sqltext,$params);
		$sqltext="delete 
		from  timecode where asset_id=?";
		$params = array();
		array_push($params,sql_escape($id));
		db_change_query($conn,$sqltext,$params);
		$sqltext="delete 
		from msc_number where asset_id=?";
		$params = array();
		array_push($params,sql_escape($id));
		db_change_query($conn,$sqltext,$params);
		 header("location:".$site_http."admin");
	}
	else
	{
		 
		$sqltext="select institute_id from users  WHERE id=?";
		$params = array();
		
		array_push($params,sql_escape($_SESSION['user_id']));
		$result = db_select_query($conn2,$sqltext,$params);
	
		$institute_id='';
		while($row = db_fetch_array($result))
		{
			$institute_id = $row['institute_id'];
		}
		if($institute_id  !='')
		{
			 $sqltext="select title, 
											abstract, 
											to_char(asset.lecture_date,'mm/dd/yyyy') as lecture_date,  
											duration, 
											asset_type, 
											abstract_url, 
											remote_url, 
											location, 
											event_title, 
											to_char(asset.created_datetime,'mm/dd/yyyy') as created_datetime, 
											created_by,
											to_char(asset.last_modified_datetime,'mm/dd/yyyy') as last_modified_datetime,
											 last_modified_by,
											rating, 
											views,
											thumbnail,
											thumbnail_type,
											assetSHA1,
											timecodeSHA1
											from asset WHERE id=? and institute_id=?";
			$params = array();
			
			array_push($params,sql_escape($id));
			array_push($params,sql_escape($institute_id));
		//	array_push($params,sql_escape($user_id));
			$result = db_select_query($conn,$sqltext,$params);
			while($row = db_fetch_object($result))
			{
				foreach ($row as $key => $value) 
				{
					$value = trim($value);
					$$key = $value;
				}
			}		
			if($thumbnail_type !='' && $thumbnail!='')//has thumbail
			{
			 $image_name =$id."_thumb.".$thumbnail_type;
			 $thumbnail_image_file = $_SERVER['DOCUMENT_ROOT']."/math-videos/images/".$institute_id."/".$id."/".$image_name;	
			 if(file_exists($thumbnail_image_file))
			 {
					unlink($thumbnail_image_file);
			 }
			}
		
			$sqltext="delete 
			from asset where id=? and institute_id =?";
			$params = array();	
			array_push($params,sql_escape($id));
			array_push($params,sql_escape($institute_id));
			db_change_query($conn,$sqltext,$params);
			$sqltext="delete 
			from presenter where asset_id=?";
			$params = array();
			array_push($params,sql_escape($id));
			db_change_query($conn,$sqltext,$params);
			$sqltext="delete 
			from keyword where asset_id=?";
			$params = array();
			array_push($params,sql_escape($id));
			db_change_query($conn,$sqltext,$params);
			$sqltext="delete 
			from  timecode where asset_id=?";
			$params = array();
			array_push($params,sql_escape($id));
			db_change_query($conn,$sqltext,$params);
			$sqltext="delete 
			from msc_number where asset_id=?";
			$params = array();
			array_push($params,sql_escape($id));
			db_change_query($conn,$sqltext,$params);	
		 header("location:".$site_http."admin");
		}
	}
}

?>